﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: TestBase.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using BOL.Maths.Distributions;

namespace BOL.Maths.Statistics.HypothesisTest
{
    public abstract class TestBase : ITest
    {
        #region Protected variables

        protected IUnivariateDistribution<double> Distribution;

        #endregion

        #region Public properties

        public TestType Type { get; private set; }

        public double Alpha { get; set; }

        public double NullValue { get; private set; }

        public virtual double TestStatistic { get { return (PointEstimate() - NullValue) / PointEstimateStandardError(); } }

        public virtual double PValue
        {
            get
            {
                var cdf = Distribution.Cdf(TestStatistic);
                double pValue;

                switch (Type)
                {
                    case TestType.Upper:
                        pValue = 1 - cdf;
                        break;
                    case TestType.Lower:
                        pValue = cdf;
                        break;
                    default:
                        pValue = 2 * (1 - Math.Max(cdf, 1.0 - cdf));
                        break;
                }

                return pValue;
            }
        }

        public bool IsSignificant { get { return PValue <= Alpha; } }

        public virtual ConfidenceInterval ConfidenceInterval
        {
            get
            {
                var pointEstimate = PointEstimate();

                switch (Type)
                {
                    case TestType.TwoSided:
                        var error = Distribution.Quantile(1 - Alpha / 2.0) * PointEstimateStandardError();
                        return new ConfidenceInterval(pointEstimate - error, pointEstimate + error);
                    case TestType.Upper:
                        return new ConfidenceInterval(double.NegativeInfinity,
                            pointEstimate - Distribution.Quantile(Alpha) * PointEstimateStandardError());
                    default:
                        return new ConfidenceInterval(
                            pointEstimate + Distribution.Quantile(Alpha) * PointEstimateStandardError(),
                            double.PositiveInfinity);
                }
            }
        }

        #endregion

        #region Constructor

        protected TestBase(TestType type, double alpha, double nullValue)
        {
            if (alpha <= 0 || alpha > 1)
                throw new ArgumentOutOfRangeException("alpha", "Alpha must be in interval [0, 1].");

            Alpha = alpha;
            Type = type;
            NullValue = nullValue;
        }

        #endregion

        #region Protected methods

        protected abstract double PointEstimate();

        protected abstract double PointEstimateStandardError();

        #endregion
    }
}
